﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Text.RegularExpressions;
using System.Net;
public partial class Manage_Cms_CatchParseDebug : System.Web.UI.Page
{
    private static RegexOptions regex = RegexOptions.IgnoreCase | RegexOptions.Multiline;

    public KeRui.Model.CmsServer ServerInfo = new KeRui.Model.CmsServer();

    public int step = PageRequest.GetInt("step", -1);

    public int ParseID = PageRequest.GetInt("ParseID", -1);

    //Catch是采集参数
    public string CatchUrl = PageRequest.GetString("CatchUrl");
    public string CatchStartPage = PageRequest.GetString("CatchStartPage");
    public string CatchBlacklist = PageRequest.GetString("CatchBlacklist");
    public string CatchWhilelist = PageRequest.GetString("CatchWhilelist");
    public int CatchSavePic = PageRequest.GetInt("CatchSavePic", 0);

    public string ListEncoding = PageRequest.GetString("ListEncoding");
    public string ListParseBegin = PageRequest.GetString("ListParseBegin");
    public string ListParseEnd = PageRequest.GetString("ListParseEnd");
    public string ListParseTags = PageRequest.GetString("ListParseTags");
    public string ListParseAttrs = PageRequest.GetString("ListParseAttrs");
    public string ListParseRule = PageRequest.GetString("ListParseRule");
    public string ContentUrl = PageRequest.GetString("ContentUrl");
    public string ContentEncoding = PageRequest.GetString("ContentEncoding");
    public string ContentParseBegin = PageRequest.GetString("ContentParseBegin");
    public string ContentParseEnd = PageRequest.GetString("ContentParseEnd");
    public string ContentParseTags = PageRequest.GetString("ContentParseTags");
    public string ContentParseAttrs = PageRequest.GetString("ContentParseAttrs");
    public string ContentParseRule = PageRequest.GetString("ContentParseRule");
    public string ContentPageParseRule = PageRequest.GetString("ContentPageParseRule");



    void CatchContentText(string htmlData, string url, string title, string pic)
    {
        string htmlDataFull = htmlData;

        //输出调试
        #region step 6
        if (step == 6)
        {
            Response.Write(htmlData);
            Response.End();
        }
        #endregion

        //定位内容
        htmlData = Utils.GetContent(htmlData, ContentParseBegin, ContentParseEnd);
        #region step 7
        if (step == 7)
        {
            Response.Write(htmlData);
            Response.End();
        }
        #endregion

        //XHTML格式化
        htmlData = Utils.FilterXss(htmlData, ContentParseTags, ContentParseAttrs);
        #region step 8
        if (step == 8)
        {
            Response.Write(htmlData);
            Response.End();
        }
        #endregion

        //正则匹配内容
        MatchCollection matches = Regex.Matches(htmlData, ContentParseRule, regex);

        //捕获自定义表单或获得内容（正则）
        MatchCollection matches2 = Regex.Matches(ContentParseRule, @"\(\?\<(?<fieldname>[0-9a-zA-Z]*?)\>(?<pattern>[\s\S]*?)\)", regex);

        foreach (Match match in matches)
        {
            GroupCollection Items = match.Groups;

            //内容保存
            string PostStr = "_act=catch";
            PostStr += "&_nodeid=0";

            //处理catchs里的设置
            int ContentState = 1;//0:草稿 1:待审核 2:审核成功 3:审核失败
            int PublicState = 0;//发布状态 0:待发布 1:发布中 2:自动发布

            string Title = "";
            string PicUrl = "";
            string Context = "";//对应Content

            foreach (Match match2 in matches2)
            {
                GroupCollection Items2 = match2.Groups;
                string FieldName = Items2["fieldname"].Value.Trim().ToLower();
                string Pattern = Items2["pattern"].Value.Trim();
                string Content = Items[FieldName].Value.Trim();

                //捕获指定字段Title内容
                if (FieldName == "title")
                    Title = Content;
                //捕获指定字段PicUrl内容
                if (FieldName == "picurl")
                    PicUrl = Content;
                //捕获指定字段Content内容
                if (FieldName == "content")
                    Context = Content;

                //【所有字段】加载内容过滤
                #region step 10
                if (step == 10)
                {
                    #region CmsCatchParseRegex
                    if (ParseID > 0)
                    {
                        KeRui.BLL.CmsCatchParseRegex bllRegex = new KeRui.BLL.CmsCatchParseRegex();
                        foreach (KeRui.Model.CmsCatchParseRegex model in bllRegex.GetList(ParseID))
                        {
                            if (FieldName == model.FieldName.ToLower())
                            {
                                Content = Regex.Replace(Content, model.Pattern, model.Replacement, regex);
                            }
                        }
                    }
                    #endregion
                }
                #endregion

                //【content】检查特殊Hack
                #region
                if (FieldName == "content")
                {
                    if (KeRui.BLL.CmsCatchHack.CheckHack(Content))
                    {
                        //163flv
                        if (KeRui.BLL.CmsCatchHack.GetHackName(Content) == "163flv")
                        {
                            //真实数据
                            Content = KeRui.BLL.CmsCatchHack.GetHackContent(Content);

                            PicUrl = KeRui.BLL.CmsCatchHack.Hack163FlvPic(Content);
                            /*
                            //获得LinkUrl
                            string LinkUrl = KeRui.BLL.CmsCatchHack.Hack163FlvPic(Content);
                            //下载
                            LinkUrl = KeRui.BLL.CmsCatchHack.SavePic(url, LinkUrl, ServerInfo);
                            //输出
                            PostStr += "&linkurl=" + Utils.UrlEncode(LinkUrl, "utf-8");
                            Response.Write("<span style='color:blue'><b>linkurl</b></span>：" + LinkUrl + "\r\n\r\n\r\n\r\n<br>\r\n\r\n\r\n\r\n");
                            */
                            //Hack数据
                            Content = KeRui.BLL.CmsCatchHack.Hack163Flv(Content);
                        }
                    }
                }
                #endregion

                //【Context】1、抓取关键词黑名单Blacklist。逗号隔开，不入库。
                #region Blacklist
                if (Context != "")
                {
                    string[] Blacklist = Utils.SplitString(CatchBlacklist, ",");
                    for (int i = 0; i < Blacklist.Length; i++)
                    {
                        if (Blacklist[i] != "")
                        {
                            if (Context.ToLower().Contains(Blacklist[i].ToLower()))
                                return;
                        }
                    }
                }
                #endregion

                //【Context】1、抓取关键词白名单Whilelist。逗号隔开，自动审核+发布
                #region Whilelist
                if (Context != "")
                {
                    if (CatchWhilelist.ToLower().Trim() == "all")
                    {
                        ContentState = 2;
                        PublicState = 1;
                    }
                    else
                    {
                        string[] Whilelist = Utils.SplitString(CatchWhilelist, ",");
                        for (int i = 0; i < Whilelist.Length; i++)
                        {
                            if (Whilelist[i] != "")
                            {
                                if (Context.ToLower().Contains(Whilelist[i].ToLower()))
                                {
                                    ContentState = 2;
                                    PublicState = 1;
                                }
                            }
                        }
                    }
                }
                #endregion

                //1图片保存
                #region SavePic
                if (CatchSavePic == 1)
                {
                    MatchCollection matchesImg = Regex.Matches(Content, @"<img([\s\S]*?)src=""(?<img>[\s\S]*?)""([\s\S]*?)>", regex);
                    #region 图片存储
                    foreach (Match matchImg in matchesImg)
                    {
                        GroupCollection ItemsImg = matchImg.Groups;

                        string ImgUrl = KeRui.BLL.CmsCatchHack.SavePic(url, ItemsImg["img"].Value.Trim(), ServerInfo);
                        //<img([\s\S]*?)src无效，得使用<img src，但是中间有可能有内容，所以先用Replace，不用Regex.Replace
                        //Content = Regex.Replace(Content, @"<img([\s\S]*?)src=""" + ItemsImg["img"].Value.Trim() + @"""([\s\S]*?)>", @"<img src=""" + ImgUrl + @""" />", regex);
                        Content = Content.Replace(ItemsImg["img"].Value.Trim(), ImgUrl);
                    }
                    #endregion

                }
                #endregion



                PostStr += "&" + FieldName + "=" + Utils.UrlEncode(Content, "utf-8");

                Response.Write("<span style='color:blue'><b>" + FieldName + "</b></span>：" + Content + "\r\n\r\n\r\n\r\n<br>\r\n\r\n\r\n\r\n");

            }



            //PicUrl修改
            if (PicUrl == "")
                PicUrl = pic;
            if (PicUrl != "")
            {

                //下载
                PicUrl = KeRui.BLL.CmsCatchHack.SavePic(url, PicUrl, ServerInfo);
                //输出
                PostStr += "&picurl=" + Utils.UrlEncode(PicUrl, "utf-8");
                Response.Write("<span style='color:blue'><b>picurl</b></span>：" + PicUrl + "\r\n\r\n\r\n\r\n<br>\r\n\r\n\r\n\r\n");
            }

            //内容状态
            PostStr += "&contentstate=" + ContentState + "";
            PostStr += "&publicstate=" + PublicState + "";



            //通信密钥
            string ServerKey = ServerInfo.ServerKey;

            PostStr += "&_m=" + Utils.MD5(ServerKey + ServerInfo.ServerIP);//ServerInfo全局变量

        }

        //检查分页
        #region
        if (ContentPageParseRule != "")
        {
            string NextUrl = "";
            MatchCollection matchesPage = Regex.Matches(htmlDataFull, ContentPageParseRule, regex);
            foreach (Match matchPage in matchesPage)
            {
                GroupCollection ItemsPage = matchPage.Groups;
                NextUrl = ItemsPage["url"].Value.Trim();
                //Response.Write("下一页：" + NextUrl+"<hr>");
            }
            if (NextUrl != "")
            {
                NextUrl = KeRui.BLL.CmsCatchHack.HackUrl(url, NextUrl);
                string htmlData2 = KeRui.BLL.CmsCatchHack.GetHtml(NextUrl, ContentEncoding);

                if (htmlData2 == "")
                {
                    Response.Write("下载失败，无法获得HTML。URL：" + NextUrl);
                    Response.End();
                }
                Response.Write("<hr>分页：" + NextUrl + "<hr>");
                CatchContentText(htmlData2, url, title, pic);
            }
        }
        #endregion

    }


    void CatchContentList(string htmlData)
    {
        //定位内容
        htmlData = Utils.GetContent(htmlData, ListParseBegin, ListParseEnd);
        #region step 2
        if (step == 2)
        {
            Response.Write(htmlData);
            Response.End();
        }
        #endregion

        //XHTML格式化

        #region CDATA 过滤
        MatchCollection matchesCDATA = Regex.Matches(htmlData, @"<(?<title1>[a-z]+)>[\s]*?<!\[CDATA\[(?<cdata>[^>]*?)\]\]>[\s]*?</(?<title2>[a-z]+)>", regex);
        foreach (Match matchCDATA in matchesCDATA)
        {
            GroupCollection ItemsCDATA = matchCDATA.Groups;
            htmlData = htmlData.Replace(@"<" + ItemsCDATA["title1"].Value + @"><![CDATA[" + ItemsCDATA["cdata"].Value + @"]]></" + ItemsCDATA["title2"].Value + @">", @"<" + ItemsCDATA["title1"].Value + @">" + ItemsCDATA["cdata"].Value + @"</" + ItemsCDATA["title2"].Value + @">");
        }
        #endregion

        htmlData = Utils.FilterXss(htmlData, ListParseTags, ListParseAttrs);
        #region step 3
        if (step == 3)
        {
            Response.Write(htmlData);
            Response.End();
        }
        #endregion

        #region step 4,5
        if (step >= 4)
        {
            //正则匹配列表
            MatchCollection matches = Regex.Matches(htmlData, ListParseRule, regex);

            if (matches.Count == 0)
            {
                Response.Write("没有分析到文章列表。");
                Response.End();
                return;
            }


            int iii = 1;
            foreach (Match match in matches)
            {
                GroupCollection Items = match.Groups;

                #region step 4
                if (step == 4)
                {
                    for (int i = 0; i < Items.Count - 1; i++)
                    {
                        Response.Write("{" + i + "}：" + Items[i + 1].Value.Trim() + "<br>\r\n\r\n");
                    }
                    Response.Write("\r\n\r\n<hr>\r\n\r\n");
                }
                #endregion

                string Url2 = ContentUrl;
                for (int i = 0; i < Items.Count - 1; i++)
                {
                    Url2 = Url2.Replace("{" + i + "}", Items[i + 1].Value.Trim());
                }

                //获得标题
                string Title = Items["title"].Value.Trim();
                //移除HTML标签
                Title = Utils.RemoveHtml(Title);

                //获得图片
                string Pic = Items["pic"].Value.Trim();
                //修正图片
                if (Pic != "")
                    Pic = KeRui.BLL.CmsCatchHack.HackUrl(Url2, Pic);

                #region step 5
                if (step == 5)
                {
                    Response.Write("<span style='color:blue'><b>地址</b></span>：" + Url2 + " [<a href='" + Url2 + "' target=_blank>查看</a>]\r\n\r\n<br>\r\n\r\n");
                    Response.Write("<span style='color:blue'><b>标题</b></span>：" + Title + "\r\n\r\n<br>\r\n\r\n");
                    if (Pic != "")
                        Response.Write("<span style='color:blue'><b>图片</b></span>：" + Pic + " [<a href='" + Pic + "' target=_blank>查看</a>]\r\n\r\n<br>\r\n\r\n");
                    Response.Write("\r\n\r\n<hr>\r\n\r\n");
                }
                #endregion

                if (step >= 6)
                {
                    //采集内容
                    if (iii == PageRequest.GetInt("ContentIndex", 0))
                    {
                        WebClient client = new WebClient();
                        htmlData = KeRui.BLL.CmsCatchHack.GetHtml(Url2, ContentEncoding);

                        if (htmlData == "")
                        {
                            Response.Write("下载失败，无法获得HTML。URL：" + Url2);
                            Response.End();
                        }

                        CatchContentText(htmlData, Url2, Title, Pic);
                    }
                }

                iii++;

            }
        }
        #endregion

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();

        //加载服务器信息
        KeRui.BLL.CmsServer bllServer = new KeRui.BLL.CmsServer();
        ServerInfo = bllServer.GetModelByCache(PageRequest.GetServerIP());

        string htmlData = KeRui.BLL.CmsCatchHack.GetHtml(CatchUrl.Replace("{page}", CatchStartPage.ToString()), ListEncoding);
        if (htmlData == "")
        {
            Response.Write("下载失败，无法获得HTML。URL：" + CatchUrl);
            Response.End();
        }
        else
        {
            #region step 1
            if (step == 1)
            {
                Response.Write(htmlData);
                Response.End();
            }
            #endregion
        }

        if (step >= 2)
        {
            CatchContentList(htmlData);
        }

        Response.End();
    }

}
